JBoss Community Archive (Read Only)

Errai

Bus Lifecycle

Turning Server Communication On and Off

By default, Errai's client-side message bus attempts to connect to the server as soon as the ErraiBus module has been loaded. The bus will stay connected until a lengthy (about 45 seconds) communication failure occurs, or the web page is unloaded.

The application can affect bus communication through two mechanisms:

  1. By setting a global JavaScript variable erraiBusRemoteCommunicationEnabled = false before the GWT scripts load, bus communication with the server is permanently disabled

  2. By calling ((ClientMessageBus) ErraiBus.get()).stop(), the bus disconnects from the server

To resume server communication after a call to ClientMessageBus.stop() or after communication with the server has exceeded the bus' retry timeout, call ((ClientMessageBus) ErraiBus.get()).init(). You can use a BusLifecycleListener to monitor the success or failure of this attempt. See the next section for details.

Observing Bus Lifecycle State and Communication Status

In a perfect world, the client message bus would always be able to communicate with the server message bus. But in the real world, there's a whole array of reasons why the communication link between the server and the client might be interrupted.

On its own, the client message bus will attempt to reconnect with the server whenever communication has been disrupted. Errai applications can monitor the status of the bus' communication link (whether it is disconnected, attempting to connect, or fully connected) through the BusLifecycleListener interface:

class BusStatusLogger implements BusLifecycleListener {

  @Override
  public void busAssociating(BusLifecycleEvent e) {
    GWT.log("Errai Bus trying to connect...");
  }

  @Override
  public void busOnline(BusLifecycleEvent e) {
    GWT.log("Errai Bus connected!");
  }

  @Override
  public void busOffline(BusLifecycleEvent e) {
    GWT.log("Errai Bus trying to connect...");
  }

  @Override
  public void busDisassociating(BusLifecycleEvent e) {
    GWT.log("Errai Bus going into local-only mode.");
  }
}

To attach such a listener to the bus, make the following call in client-side code:

ClientMessageBus bus = (ClientMessageBus) ErraiBus.get();
bus.addLifecycleListener(new BusStatusLogger());
JBoss.org Content Archive (Read Only), exported from JBoss Community Documentation Editor at 2020-03-10 12:34:47 UTC, last content change 2013-01-25 18:33:42 UTC.